#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>
using namespace std;
#define NUM 5
#define TICKETNUM 1000
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t full = PTHREAD_COND_INITIALIZER;
struct ticket
{
public:
    int size;
    ticket(int num = TICKETNUM)
        : size(num)
    {
    }

public:
    
    void buyticket(int t)
    {
        pthread_mutex_lock(&mtx);
        while (size == 0)
        {
            pthread_cond_signal(&full);
            pthread_cond_wait(&empty, &mtx);
        }
        size--;
        cout << t << ":买票成功,还剩余:" << size << endl;
        pthread_mutex_unlock(&mtx);
    }
    void renenw()
    {
        pthread_mutex_lock(&mtx);
        while (size <= TICKETNUM && size != 0)
        {
            pthread_cond_signal(&empty);
            pthread_cond_wait(&full, &mtx);
        }
        size = TICKETNUM;
        pthread_mutex_unlock(&mtx);
    }
};
